import requests
from lxml import etree
import mysql
#啪city主页+cityname和citylink
def pacong():
    url = "https://www.jianzhimao.com/ctrlcity/changeCity.html"
    headers = {
        'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36 Edg/95.0.1020.44'
    }
    rep = requests.get(url=url, headers=headers)
    cityHtml = rep.text
    tree = etree.HTML(cityHtml)
    cityName = tree.xpath('//ul[@class="city_table"]/li/a/text()')
    citylink = tree.xpath('//ul[@class="city_table"]/li/a/@href')
    dict_data=dict(zip(cityName,citylink))
    # chuangjianbiao()
    for name,link in dict_data.items():
        tianjiasuju(name, link)
#创建表已废除
# def chuangjianbiao():
#     sql="""
#     create table if not exists cityinfo(
#     id int(10) not ull auto_increment primary,
#     cityName varchar(10) not null ,
#     cityurl varchar(100) not null)
#     """
#     mysql.chuangjianbiao(sql=sql)
#添加塑胶
def tianjiasuju(cityName,citylink):
    sql="""
    insert into cityinfo(cityName,citylink)
    values
    ('%s','%s')
    """%(cityName,citylink)
    mysql.gengxinbiao(sql)
if __name__ == '__main__':
    pacong()
